package com.cloud.demo.login.util;

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

/**
 * RSA加密解密工具
 *
 * @author xxg
 * @date 2020-09-26
 */
public class RSAUtil {

    public static final String PUB_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDwryKlzk4EY7dPXg3+6uYbm/hu" +
            "gJFtrIyDTGE7/l8TrgvV0Vd/2oYklem0Yy5++se3wGc7AlB0ufVJCXIlSTB0VjjZ" +
            "mC++hN1AYEyLTkwcRpQMoiJmj+tuPklQGAhpWkSFge3JG4xqMFL2VBIVKwfrU09v" +
            "hfLpj9l8MToKmQyqMQIDAQAB";
    public static final String PRI_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAPCvIqXOTgRjt09e" +
            "Df7q5hub+G6AkW2sjINMYTv+XxOuC9XRV3/ahiSV6bRjLn76x7fAZzsCUHS59UkJ" +
            "ciVJMHRWONmYL76E3UBgTItOTBxGlAyiImaP624+SVAYCGlaRIWB7ckbjGowUvZU" +
            "EhUrB+tTT2+F8umP2XwxOgqZDKoxAgMBAAECgYAl7fOv3Ys99dj1kQTzn2EUCMhl" +
            "4a0Jc5/RMUBBjBy/pTYIRC1d4LozGczyuI6jRDIlKxUZn4u+WW/mSq7KYXZZEOBh" +
            "RYT55C0lqtsrRIwhhk387lJ7Bh848+JKcl0hNrAA9rpHou5o/kmtRFq+V5AoJbU9" +
            "S42XhD9+4CR1BFA2AQJBAPn+G9nvfj/dZEJts1iK2yQv5mztDvOfsBDFUb+FNKB8" +
            "sKAZ0tqTCkfN/D+tMD58QCfzv6lyPYj6zqH43+93pZkCQQD2d8NY9xgvaLZwvMh/" +
            "+2clk3Y2aaiy2I5PvYps8o9Al3S9dZ0W+9ru/zzBgZSOly1N7Sllpt3zqNzDUuKp" +
            "c9hZAkEAlfPtouE8WnSjIhGJZZAM+Pzee/IKaOodg0HI0lfFhIqmq7Ixvzg6D24o" +
            "Y82vxZtkJFLaYXqMHLFM5u1Q7eRX6QJASeMndgFFh9jtcKi3tt5oUrR9aNyODWes" +
            "ai1ZVUX2OhftkGq0+FHylVmBHkW5gnHBzC3ADlBTE5vY6tlrR/+JqQJBANkbxqfV" +
            "Y9QthntOesFU0Sux2nvYEtM7tSGVITVmtZfxlqhpicKac4pEKjbwZbQMU6WCnHrw" +
            "sq70L6DAqwQrT9Y=";

    /**
     * 加密手机号解密(私钥解密)
     *
     * @param cryptograph 手机号密文
     * @param prikey      私钥
     * @return 解密手机号
     * @throws Exception 异常信息
     */
    public static String decrypt(String cryptograph, String prikey) throws Exception {
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(prikey));
        PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        byte[] b = Base64.getDecoder().decode(cryptograph);
        return new String(cipher.doFinal(b));
    }

    /**
     * RSA公钥加密
     *
     * @param str       明文
     * @param publicKey 公钥
     * @return 密文
     * @throws Exception 异常信息
     */
    public static String encrypt(String str, String publicKey) throws Exception {
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey));
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(keySpec);
        //RSA加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        return Base64.getEncoder().encodeToString(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
    }
}
